package com.cocotingo.snail.samples;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;

import javax.swing.JFrame;

import com.cocotingo.snail.Animation;
import com.cocotingo.snail.AnimationFunction;
import com.cocotingo.snail.MouseWheelEvent;
import com.cocotingo.snail.PaintMode;
import com.cocotingo.snail.View;
import com.cocotingo.snail.ViewContext;

public class SampleMain5 {

	public static void main(String[] args) {
		JFrame mainFrame = new JFrame("JDAT Visual Tool");
		mainFrame.setSize(800, 600);
		mainFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		ViewContext context = new ViewContext(mainFrame.getContentPane());
		context.getRootView().addSubview(new View(100, 100, 100, 100) {
			{
				addSubview(new View(5, 5, 10, 10));
				addSubview(new View(85, 5, 10, 10));
				addSubview(new View(5, 85, 10, 10));
				addSubview(new View(85, 85, 10, 10));
				
				setBackgroundColor(Color.yellow);
				new Animation(5.0f, AnimationFunction.EASE_IN_EASE_OUT) {
					@Override
					protected void animation(float progress) {
						AffineTransform t = AffineTransform.getRotateInstance(progress * 2.0f * Math.PI, 50, 50);
						t.translate(50, 50);
						t.scale(progress + 1, progress + 1);
						t.translate(-50, -50);
						setTransform(t);
					}
					protected void completed(boolean canceled) {
						commit(); // auto-replay
					}
				}.commit();
			}
		});
		
		context.getRootView().addSubview(new View(200, 200, 200, 200) {
			
			View contentView;
			
			{
				setClipped(true);
				setBackgroundColor(Color.orange);
				setTransform(AffineTransform.getRotateInstance(3.14 / 3.0));
				contentView = new View(0, 0, 200, 500);
				contentView.setPaintMode(PaintMode.DISABLED);
				addSubview(contentView);
				
				for (int i = 0; i < 10; i++) {
					contentView.addSubview(new View(5, 5 + i * 50, 190, 45) {
						{
							setBackgroundColor(Color.red);
						}
					});
				}
			}
			
			@Override
			protected void postMouseWheelMoved(MouseWheelEvent e) {
				e.handle();
				contentView.setTop(contentView.getTop() + e.getRotation());
			}
			
		});
		
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		context.prepareContent(d.width, d.height);
		mainFrame.setVisible(true);
	}

}
